function performances() {
    var sId = $("#welcome").find("input[name=sId]").val();
    $.post("../performance/performances",
        {"sectionId": sId},
        function (data) {
            if (!$.isEmptyObject(data)) {
                var $tb = $("#myTable2").find("tbody");
                $tb.html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    var po = data[i].resume.position;
                    $tb.append("<tr><td>" + data[i].id + "</td>" +
                        "<td>" + data[i].resume.realName + "</td>" +
                        "<td>" + po.section.section + " " + po.position + "</td>" +
                        "<td>" + data[i].performance + "</td></tr>");
                }
                $("#myTable2").find("tfoot").hide();
            } else {
                sectionEmployees();
            }
        });
}

function getGrantMonth() {
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth();

    if (m < 10)
        m = "0" + m;
    if (m === 0) {
        m = 12;
        y -= 1;
    }
    var d = now.getDate();
    if (d < 10)
        d = "0" + d;

    var str = y + "-" + m + "-" + d;
    console.log(str);

    $("#myTable2").find("input[class=grantMonth]").each(function () {
        $(this).val(str);
    });

    $("input[class=grantMonth]").each(function () {
        console.log("$(\"input[class=grantMonth]\").each --- " + $(this).val());
    });
}

function performanceManage() {
    var now = new Date();
    var m = now.getDate() + 1;
    if (m < 15)
        alert("请在每月15号之后进行操作：查询或发放上月绩效！");
    else {
        performances();
        switchoverColor(this, $("#admin_performance"));
    }
}

function sectionEmployees() {
    var sId = $("#welcome").find("input[name=sId]").val();
    var $tb = $("#myTable2").find("tbody");
    $.post("../admin/sectionEmployees",
        {"section.id": sId},
        function (data) {
            if (!$.isEmptyObject(data)) {
                $tb.html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    $tb.append("<tr><td>" + data[i].id + "</td>" +
                        "<td>" + data[i].resume.realName + "</td>" +
                        "<td>" + data[i].resume.position.section.section + " " + data[i].resume.position.position + "</td>" +
                        "<td><input type='hidden' value='" + data[i].resume.id + "' name='performances[" + i + "].resume.id'>" +
                        "<input type='hidden' name='performances[" + i + "].grantMonth' class='grantMonth'>" +
                        "<input type='text' name='performances[" + i + "].performance' class='performance_input'></td></tr>")
                }
                $tb.find("td").css("text-align", "center");
                getGrantMonth();
            } else {
                $tb.html("<tr><td colspan='4'>还没有相关消息！</td></tr>");
            }
        });
}

function checkPerformanceSubmit() {
    var arr = new Array();
    var reg = /^\d*$/;
    var flag = 0;
    $("input[class=performance_input]").each(function (index) {
        var p = $(this).val();
        console.log("$(\"input[class=grantMonth]\").each --- " + p);
        arr[index] = p;
        // var reg = /^\d*$/;
        //
        // if (p === "") {
        //     alert("还有绩效未出入，请输入绩效！");
        //     return false;
        // }
        //
        // if (reg.test(p) === false) {
        //     alert("请正确输入绩效！");
        //     return false;
        // }
        // return true;
    });
    for (var i = 0, n = arr.length; i < n; i++) {
        console.log("arr[i] = " + arr[i]);
        if (arr[i] === "") {
            flag = 1;
            break;
        }
        if (reg.test(arr[i]) === false) {
            flag = 2;
            break;
        }
    }
    if (flag === 0)
        return true;

    if (flag === 1) {
        alert("还有绩效未出入，请输入绩效！");
        return false;
    }

    if (flag === 2) {
        alert("请正确输入绩效！");
        return false;
    }
}